package com.ssy.lingxi.workflow.handler.config;

/**
 * @author jw.chen
 * @version 2.0.0
 * @description Feign拦截器配置
 * @date 2022/2/24
 */

import com.ssy.lingxi.common.constant.HeaderParamEnum;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

@Configuration
public class FeignConfiguration implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if(attributes != null){
            HttpServletRequest request = attributes.getRequest();
            Enumeration<String> headerNames = request.getHeaderNames();
            if (headerNames != null) {
                while (headerNames.hasMoreElements()) {
                    String name = headerNames.nextElement();
                    if(HeaderParamEnum.ACCEPT_LANGUAGE.getCode().equals(name)){
                        String values = request.getHeader(name);
                        template.header(name, values);
                    }
                }
            }
        }
    }
}
